package com.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.SaleBean;
import com.project.bean.UserBean;
import com.project.service.ISaleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;

@RestController
@RequestMapping("sale")
@Api(tags = "销售订单控制器")
public class SaleController {
    @Autowired
    private ISaleService saleService;
    @ApiOperation("添加销售订单")
    @RequestMapping("add")
    @RequiresPermissions("sale:add")
    public String add(SaleBean sale, HttpServletRequest req){
        UserBean loginUser = (UserBean)req.getSession().getAttribute("loginUser");
        saleService.add(sale,loginUser.getId());
        return "ok";
    }
    @ApiOperation(value = "动态分页查询销售记录",notes = "按页码、客户名称、起始日期、结束日期分页查询销售信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNO",value = "页码",required = true),
            @ApiImplicitParam(name = "client",value = "客户名称"),
            @ApiImplicitParam(name = "startDate",value = "起始日期"),
            @ApiImplicitParam(name = "endDate",value = "结束日期"),
    })
    @RequestMapping("cutByItem")
    @RequiresPermissions("sale:find")
    public IPage<SaleBean> cutByItem(Integer pageNO, String client, LocalDate startDate, LocalDate endDate){
      return  saleService.cutByItem(pageNO,client,startDate,endDate);
    }
    @ApiOperation("按编号查询销售信息")
    @ApiImplicitParam(name="id",value = "编号")
    @RequestMapping("findById")
    @RequiresPermissions("sale:find")
    public SaleBean findById(Integer id){
        return saleService.findById(id);
    }

    @ApiOperation("获取用户销售车总价")
    @RequestMapping("getTotalByUser")
    @RequiresPermissions("sale:add")
    public Integer getTotalByUser(HttpServletRequest req){
        UserBean loginUser =(UserBean) req.getSession().getAttribute("loginUser");
        return saleService.getTotalByUser(loginUser.getId());
    }

}
